import FileSaver from 'file-saver'
import * as XLSX from 'xlsx' // vue3需要 * as
import XLSXS from 'xlsx-style'

export default function () {
  //html导出excel
  const htmlToExcel = (id, fileName) => {
    const opts = { raw: true } // 每个单元格都将保存原始字符串
    const et = XLSX.utils.table_to_book(document.getElementById(id), opts)
    const etout = XLSX.write(et, {
      bookType: 'xlsx',
      bookSST: true,
      type: 'array',
    })
    try {
      FileSaver.saveAs(
        new Blob([etout], {
          type: 'application/octet-stream',
        }),
        `${fileName}.xlsx`
      )
    } catch (e) {
      console.log(e, etout)
    }
  }

  return {
    htmlToExcel,
  }
}
